package com.fastmobile.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fastmobile.entity.Transactions;
import com.fastmobile.impl.TransactionsImpl;

@Controller
public class UTService {
	
	@RequestMapping("/index.html")
	public String index(){
		return "index";
	}
	
	@RequestMapping("/home.html")
	public ModelAndView showHome(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndView mav = new ModelAndView("home");
//		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
//		EntityManager em = emf.createEntityManager();
//		TransactionsImpl transactionImpl = new TransactionsImpl();
//		transactionImpl.setEntityManager(em);
//		response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out;
//        List<JSONObject> objList = new ArrayList<JSONObject>();
//        
//        
//        try {
//
//            objList = transactionImpl.getJsonLockedTransaction(2);
//            out = response.getWriter();
//            out.println("<div id='mobileId'>");
//            out.println(objList);
//            out.println("</div>");
//            out.flush();
//        } catch (JSONException ex) {
//            // TODO Auto-generated catch block
//            ex.printStackTrace();
//        } catch (IOException ex) {
//            // TODO Auto-generated catch block
//            ex.printStackTrace();
//        }
//        JSONObject obj = new JSONObject();
//        Transactions transaction = transactionImpl.getTransactionById("SE1302/0001");
//        obj.put("transId", transaction.getTransactionId());
//        obj.put("transUnlock", transaction.isTransactionUnlock());
//        obj.put("dateofPrealert", transaction.getDateofPrealert());
//        try {
//            out = response.getWriter();
//            out.println("<div id='mobileId'>");
//            out.println(obj);
//            out.println("</div>");
//            out.flush();
//        } catch (IOException ex) {
//            // TODO Auto-generated catch block
//            ex.printStackTrace();
//        }
//        session.setAttribute("test", "test");
		return mav;
	}
	
	@RequestMapping("/mobilesyncdata.html")
	public String processTest(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        List<JSONObject> objList = new ArrayList<JSONObject>();
        
        
        try {

            objList = transactionImpl.getJsonLockedTransaction(2);
            out = response.getWriter();
            out.println("<div id='mobileId'>");
            out.println(objList);
            out.println("</div>");
            out.flush();
        } catch (JSONException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (IOException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
		return "home";
	}
	
	@RequestMapping("/mobilesenddata.html")
	public String sendDataToServer(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, JSONException{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String json = "";
	        if (br != null) {
	            json = br.readLine();
	        }
	    JSONObject jsonObject = new JSONObject(json);
	    String id = (String) jsonObject.get("transId");

	    Date today = new Date();
	    transactionImpl.unlockTransactionById(id, today);
	    
	    response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        out = response.getWriter();
        out.println("<div id='sendStatus'>");
        out.println(json);
        out.println("</div>");
        out.flush();
	    
		return "home";
	}
	
	

}
